package com.hw.dagl.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hw.dagl.dao.impl.DepartmentDaoImpl;
import com.hw.dagl.model.Frm_Department;
import com.hw.dagl.service.DepartmentService;
import com.hw.dao.utils.EntityTools;
import com.hw.dao.utils.OraclePageDaoBase;
import com.hw.dao.utils.Page;



@Service
public class DepartmentServiceImpl implements DepartmentService{
	@Resource
	private OraclePageDaoBase oraclePageDaoBase;
	
	@Resource
	private DepartmentDaoImpl DepartmentDaoImpl;
	
	private static Logger logger = LoggerFactory.getLogger(DepartmentServiceImpl.class);

	
	
	/**
	 * 分页查询所有部门信息
	 */
	@Override
	public Page<Frm_Department> findDepartment(Page<Frm_Department> pages,Frm_Department info) {
		StringBuffer sql=new StringBuffer(" select t.* from FRM_DEPARTMENT t where 1=1");
		List<Object> args = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(info.getGlbm())){
			sql.append(" AND t.glbm like ? ");
			args.add("%"+info.getGlbm()+"%");
		}
		if(StringUtils.isNotEmpty(info.getBmmc())){
			sql.append(" or t.bmmc like ? ");
			args.add("%"+info.getBmmc()+"%");
		}
		logger.info("分页查询所有部门信息的sql。。。"+sql);
		return oraclePageDaoBase.queryForPage(sql.toString(), pages, new RowMapper<Frm_Department>(){
			private EntityTools<Frm_Department> et = new EntityTools<Frm_Department>();
			@Override
			public Frm_Department mapRow(ResultSet rs, int arg1)throws SQLException {
				Frm_Department t = new Frm_Department();
				et.setColumnsValueByResultSet(rs, t);
				return t;
			}
		}, args.toArray());
	}
	
	
	/**
	 * 根据用户编号查询部门信息
	 */
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public Frm_Department findDepartmentByGlbm(Frm_Department info) {
		String sql=" select t.* from Frm_Department t where t.glbm= ? ";
		List<Frm_Department> list = new ArrayList<Frm_Department>();
		if(info != null){
			 logger.info("根据用户编号查询部门信息的sql。。。"+sql);
			 list= oraclePageDaoBase.query(sql, new Object[]{info.getGlbm()},new RowMapper<Frm_Department>(){
				private EntityTools<Frm_Department> et = new EntityTools<Frm_Department>();
				@Override
				public Frm_Department mapRow(ResultSet rs, int index)
						throws SQLException {
					Frm_Department t = new Frm_Department();
					et.setColumnsValueByResultSet(rs, t);
					return t;
				}
			});
		}
		
		return list.size() > 0? list.get(0) : null;
	}
	
	
	/**
	 * 批量删除部门
	 */
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean delDepartment(String ids){
		if(StringUtils.isNotEmpty(ids)){
			ids=ids.replaceAll(",", "','");
			String sql=" delete from Frm_Department d where d.glbm in ('"+ids+"') ";
			int flag=oraclePageDaoBase.update(sql);
			logger.info("删除部门信息的sql。。。"+sql);
			return flag!= -1;
		}else{
			return true;
		}
	}
	
	
	/**
	 * 新增部门信息
	 */
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean saveDepartment(Frm_Department info){
		String sql=" insert into Frm_Department (glbm,bmmc,bmqc,yzmc,fzjg,bmjb,fzr,lxr,lxdh,czhm,lxdz,sjbm,bz,jzjb,gltz,jfly, yfly,jlzt,jrgaw,lsgx,jzptglbm,csbj) "
				+ " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
		logger.info("新增部门信息的sql。。。"+sql);
		int flag=oraclePageDaoBase.update(sql, new Object[]{info.getGlbm(),info.getBmmc(),info.getBmqc(),info.getYzmc(),info.getFzjg(),
				   info.getBmjb(),info.getFzr(),info.getLxr(),info.getLxdh(),info.getCzhm(),info.getLxdz(),
				   info.getSjbm(),info.getBz(),info.getJzjb(),info.getGltz(),info.getJfly(),info.getYfly(),
				   info.getJlzt(),info.getJrgaw(),info.getLsgx(),info.getJzptglbm(),info.getCsbj()});
		return flag!=-1;
	}
	
	
	/**
	 * 修改部门信息
	 */
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean updateDepartment(Frm_Department info){
			String sql=" update Frm_Department set bmmc=?,bmqc=?,yzmc=?,fzjg=?,bmjb=?,fzr=?,lxr=?,lxdh=?,czhm=?,lxdz=?,sjbm=?,bz=?,jzjb=?,gltz=?,jfly=?,yfly=?,jlzt=?,jrgaw=?,lsgx=?,jzptglbm=?,csbj=? where glbm = ? ";
			int flag=oraclePageDaoBase.update(sql, new Object[]{info.getBmmc(),info.getBmqc(),info.getYzmc(),info.getFzjg(),
					   info.getBmjb(),info.getFzr(),info.getLxr(),info.getLxdh(),info.getCzhm(),info.getLxdz(),
					   info.getSjbm(),info.getBz(),info.getJzjb(),info.getGltz(),info.getJfly(),info.getYfly(),
					   info.getJlzt(),info.getJrgaw(),info.getLsgx(),info.getJzptglbm(),info.getCsbj(),info.getGlbm()});
			logger.info("更新部门信息的sql。。。"+sql.toString());
			return flag!=-1;
	}
	
	/* 
	 * 显示树图
	 */
	@Override
	public List<Frm_Department> queryDepartmentInfo(String glbm){
		String oldGlbm= glbm;
		StringBuffer sql = new StringBuffer("SELECT * FROM Frm_Department_local bm where 1=1 ");
		List<Object> args = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(glbm)){
			sql.append(" AND  (bm.glbm LIKE ? OR bm.glbm=?)");
			if(glbm.endsWith("0000000000")){
				glbm = glbm.substring(0, 2);
			}else if(glbm.endsWith("00000000")) {//支队判断
				glbm = glbm.substring(0, 4);
			}else if(glbm.endsWith("000000")) {//大队判断  
				glbm = glbm.substring(0, 6);
			}else if(glbm.endsWith("000010")) {
				glbm = glbm.substring(0, 6);
			}
			args.add(glbm+"%");
			args.add(oldGlbm);
		}
		sql.append("  START WITH bm.glbm =?  CONNECT BY bm.sjbm = PRIOR bm.glbm ORDER BY bm.glbm ASC");
		args.add(oldGlbm);
		List<Frm_Department> list = oraclePageDaoBase.query(sql.toString(), args.toArray(), new RowMapper<Frm_Department>(){
			private EntityTools<Frm_Department> et = new EntityTools<Frm_Department>();
			@Override
			public Frm_Department mapRow(ResultSet rs, int index) throws SQLException {
				Frm_Department t = new Frm_Department();
				et.setColumnsValueByResultSet(rs, t);
				return t;
			}
		});
		return list;
	}
}
